package top.vybfi.week2.customer;

//业务层

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import top.vybfi.week2.exception.NotFoundException;

import java.util.List;

@Service
public class CustomerService {
    private final static Logger LOGGER = LoggerFactory.getLogger(CustomerService.class);
    private final CustomerRepository customerRepository;


    @Autowired
    public CustomerService(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }


    List<User> getusers(){
        LOGGER.info("getcustomer called");
        return customerRepository.findAll();
    }

    User getuser(long id){
        return customerRepository
                .findById(id)
                .orElseThrow(() -> {
                            NotFoundException notFoundException = new NotFoundException("user" + id + "无法找到");
                            LOGGER.error("error:getuser{}"+id,notFoundException);
                            return notFoundException;

                        }
                );
    }
}
